// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import HardModeGame from "./HardModeGame";
import Score from "./Score";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Enemy extends cc.Component {

    //爆炸特效
    @property({displayName:"敌机精灵", type: cc.SpriteFrame})
    enemySpriteFrame: cc.SpriteFrame = null;

    @property({displayName:"爆炸效果", type: cc.SpriteFrame})
    explodeEffect: cc.SpriteFrame = null;

    @property({displayName:"爆炸音效", type: cc.AudioClip})
    explodeAudio: cc.AudioClip = null;

    @property({displayName:"被击中后的音效", type: cc.AudioClip})
    hurtAudio: cc.AudioClip = null;

    speedY: number = 100;

    private canvas: cc.Node = null;
    private scoreValue: number = 0;
    private healthPoint: number = 0;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        // 创建目标
        this.schedule(this.onCreatingEnemyBullet, 2, cc.macro.REPEAT_FOREVER, 0.1);
    }

    start () {

    }

    onEnable() {
        
        this.canvas = cc.find('Canvas');
        this.scoreValue = 5;
        this.healthPoint = this.scoreValue;

    }

    onCreatingEnemyBullet() {

        this.EnemyShoot();
    }

    EnemyShoot() {

        // 动态创建子弹
        let enemyBulletNode = HardModeGame.inst.createEnemyBullet();

        enemyBulletNode.setPosition(this.node.x - this.node.height/8, this.node.y - this.node.height);
        this.node.parent.addChild(enemyBulletNode); //挂在Canvas节点下面
    }

    enemyExplode() {

        cc.log('敌机爆炸');
        
        cc.audioEngine.play(this.explodeAudio, false, 1);

        // 旋转坠毁特效
        // this.node.angle = 0;
        // let self = this;
        // cc.tween(this.node)
        //   .to(0.5, {angle: 360}, {easing: 'cubieout'})
        //   .start();

        // 爆炸特效
        // 使用缓动系统tween, 在0.5s内实现
        // 爆炸效果, scale缩放从0.1 ~ 0.5, 
        // 烟消云散效果, opacity透明度从255 ~ 0
        // 当缓动系统结束后, 使用call调用一个方法
        let sp: cc.Sprite = this.node.getComponent(cc.Sprite);
        sp.spriteFrame = this.explodeEffect;

        this.node.scale = 0.1;
        this.node.opacity = 255;
        let self = this;  // 使用闭包语法
        cc.tween(this.node)
          .to(0.5, {scale: 2, opacity: 0})   
          .call( function() {
            
            self.node.scale = 1.5;
            sp.spriteFrame = self.enemySpriteFrame;
            self.node.opacity = 255;

            self.enemyDismiss();})
          .start();
    }

    enemyDismiss() {
        
        HardModeGame.inst.destroyEnemy(this.node);
        cc.log('敌机节点销毁');
    }

    update (dt) {

        let step = this.speedY * dt;
        this.node.y -= step;

        if(this.node.y < - this.canvas.height/2 - this.node.height) {

            this.enemyDismiss();

            cc.log('敌机超出视野, 自动销毁');
        }
    }

    onCollisionEnter(other: cc.Collider, self: cc.Collider) {

        if(this.healthPoint <= 0) {

            return;
        }

        if(other.node.name == '玩家子弹') {

            this.healthPoint -= 1;

            cc.audioEngine.play(this.hurtAudio, false, 1);

            if(this.healthPoint <= 0) {

                Score.i.addScore(this.scoreValue);
                this.enemyExplode();
            }   
        }
    }
}
